<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号', trigger: 'onChange', pattern: /^1[3-9]\d{9}$/  }]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="[{ required: true, message: '请填写密码', trigger: 'onChange', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
        <van-button
        round
        block
        type="info"
        native-type="submit"
        :loading="isLoading"
        :disabled="isLoading"
        loading-text="程序员拼命加载ing...">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// import { loginApi } from '@/api/index.js'
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      formLogin: {
        mobile: '13888888888',
        code: '246810' // 默认密码，一般不用改
      },
      isLoading: false // 加载状态
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit (values) {
      this.isLoading = true
      try {
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoading = false
        this.$router.replace('/layout')
      } catch (err) {
        this.isLoading = false
        Notify({ type: 'warning', message: '手机号或密码错误' })
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .van-nav-bar{
    background: #007bff;
  }

  // 如果我想改变引入组件的里面的dom上的样式直接使用class是不生效的
  // 原因因为组件上使用了scoped他在打包过程中会给dom添加个类用作样式
  // 可以通过在类名前面加上/deep/实现类名穿透改变子组件的样式

  /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
  /deep/ .van-nav-bar__title{
    color: #fff;
  }
</style>
